package com.sptpc.demo.controller;

import com.sptpc.demo.controller.requests.ModifyStudentRequest;
import com.sptpc.demo.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.rmi.runtime.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/student")

public class StudentController {

    @PostMapping("/stu")
    public Student addNewStudent(@RequestBody Student student){
        log.info("添加的学生信息：{}",student);
        return Student.builder()
                .id(7L).adminssionDate(LocalDate.parse("2020-09-01"))
                .name("张三").major("移动应用开发")
                .build();

    }
    @PostMapping("/all")
    public List<Student> getAll(){
        ArrayList<Student> results = new ArrayList<>();
        results.add(Student.builder().id(1L)
                .name("张三")
                .build());
        results.add(Student.builder().id(2L)
                .name("李四")
                .build());
        results.add(Student.builder().id(3L)
                .name("王五")
                .build());
        return  results;
    }


    @GetMapping("/{id}")
    public Student getStudent(@PathVariable("id") Integer id){
        log.info("查询学生的id: {}",id);
        Student student=Student.builder().id(1L)
                .name("张三")
                .build();
        return student;
    }

    @DeleteMapping("/{id}")
    public String deleteStudent(@PathVariable Long id){
        log.info("要删除的学生:{}",id);
        return "删除成功";
    }

    @PutMapping("/")
    public Student modifyStudent(@RequestBody ModifyStudentRequest request){
        log.info("要修改的学生：{}",request);
        Student addStudent=Student.builder().id(1L)
                .name("张三")
                .build();
        return addStudent;
    }

    @PostMapping(value = "/batch",
            consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public List<Student> batchAddNewStudent(MultipartFile file) {
        List<Student> results = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    file.getInputStream()));
            String line = "";
            while ((line = reader.readLine()) != null) {
                // 解析一行
                String[] args = line.split(",");
                Student student = Student.builder()
                        .id(Long.parseLong(args[0]))
                        .name(args[1])
                        .adminssionDate(LocalDate.parse(args[2]))
                        .major(args[3])
                        .build();
                results.add(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return results;
    }

}
